<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 头部 -->
    <TodoHeader @add="add"></TodoHeader>
    <!-- 主体 -->
    <TodoMain :list="list" @del="del"></TodoMain>
    <!-- 尾部 -->
    <TodoFooter :list="list" @clear="clear"></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      // 数据数组
      list: JSON.parse(localStorage.getItem("list")) || [
        { id: 1, name: "打篮球" },
        { id: 2, name: "踢足球" },
        { id: 3, name: "打台球" },
      ],
    };
  },
  methods: {
    // 1. 添加
    add(todoName) {
      this.list.unshift({
        id: Date.now(),
        name: todoName,
      });
    },
    // 2. 删除
    del(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    // 3. 清空
    clear() {
      this.list = [];
    },
  },
  watch: {
    list: {
      deep: true,
      handler(newValue) {
        localStorage.setItem("list", JSON.stringify(newValue));
      },
    },
  },
};
</script>

<style>
</style>
